package ${package}.dto.base;

import lombok.Data;

import java.io.Serializable;

@Data
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Boolean success = false;
    private String errorCode;
    private String errorMessage;
    private T obj;

    public R() {
    }

    public R(boolean success, String errorCode, String errorMessage, T obj) {
        this.success = success;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.obj = obj;
    }

    public R(boolean success) {
        this.success = success;
    }

    public static <T> R<T> failed() {
        return new R<>(false);
    }

    public static <T> R<T> failed(String code, String message) {
        R<T> result = new R<>();
        result.setErrorCode(code);
        result.setErrorMessage(message);
        return result;
    }

    public static <T> R<T> success(T t) {
        R<T> result = new R<>();
        result.setSuccess(true);
        result.setObj(t);
        return result;
    }

    public static <T> R<T> success() {
        return new R<>(true);
    }
}
